<!-- 用户组织关系对象，组织成员 首页 -->
<template>
  <div class="app-container">
    <el-tabs v-model="activeName" type="border-card" @tab-click="handleTabClick">
      <el-tab-pane label="成员列表" name="menbers">
        <!--工具栏-->
        <div class="head-container">
          <!-- 搜索栏-->
          <div v-if="crud.props.searchToggle">
            <el-input v-model="query.username" clearable size="small" placeholder="可输入用户名进行搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
            <rrOperation /></div>
          <!-- 按钮-->
          <crud-opts :permission="permission">
            <template slot="left">
              <el-button
                v-if="permission.has('add')"
                class="filter-item"
                size="mini"
                type="primary"
                icon="el-icon-plus"
                @click="toAdd"
              >
                新增
              </el-button>
            </template>
          </crud-opts>
        </div>
        <el-table
          v-if="tabIndex == 0"
          ref="table"
          v-loading="crud.loading"
          :data="crud.data"
          border
          style="width: 100% "
          row-key="id"
          stripe
          tooltip-effect="dark"
          :highlight-current-row="true"
          @sort-change="crud.sortChange"
          @selection-change="crud.selectionChangeHandler"
        >
          <el-table-column type="selection" width="39" />
          <!--<el-table-column type="index" width="45" />-->

          <el-table-column prop="username" label="用户名" />

          <el-table-column prop="memberName" label="姓名" />

          <!-- <el-table-column prop="stationId" label="岗位id" sortable="custom" /> -->
          <el-table-column prop="stationName" label="岗位" sortable="custom" :show-overflow-tooltip="true" />

          <!-- 智能推断为字典类型, 字典类型为 dict.sys_user_dep_position_code -->
          <el-table-column prop="positionCode" label="岗位职级" sortable="custom" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <!-- 智能推断为字典类型, 字典类型为 dict.sys_user_dep_position_code  -->
              {{ dict.label.sys_user_dep_position_code[scope.row.positionCode] || '不详/' + scope.row.positionCode }}
            </template>
          </el-table-column>

          <el-table-column prop="userEmployeeNum" label="成员编号" sortable="custom" />

          <!-- 数据权限 1 查看个人数据  2 查看本部门数据 4.当前组织以及下级数据 8:查看指定部门数据 -->
          <el-table-column prop="dataLevel" label="数据权限" sortable="custom" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              {{ getSysUserDepDataLevelLabel(scope.row.dataLevel) }}
            </template>
          </el-table-column>

          <el-table-column
            prop="status"
            label="状态"
            width="80"
            sortable="custom"
          >
            <template slot-scope="scope">
              {{ getMemberStatuslabel(scope.row.status) }}
            </template>
          </el-table-column>

          <el-table-column prop="createTime" label="加入时间" :show-overflow-tooltip="true" header-align="center" sortable="custom" />
          <el-table-column fixed="right" label="操作" width="120" header-align="center" align="center">
            <template slot-scope="scope">
              <!-- 操作按钮，简介操作的（不需要详情和扩展的），可以只引用 udOperation -->
              <udOperation
                :data="scope.row"
                :permission="permission"
              />
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页插件, 如果不需要注释掉下面一行,以及引入的 pagination 组件即可 -->
        <pagination />
      </el-tab-pane>
      <el-tab-pane label="可添加成员" name="users">
        <user-list v-if="tabIndex == 1" :dep-id="depId" @toAddMenuber="toAddMenuber" />
      </el-tab-pane>
    </el-tabs>
    <!-- 表单 -->
    <SysUserDepForm :user-id="userId" :username="username" :dep-id="depId" :dep-name="depName" :nickname="nickname" />
    <!-- 详情, 如果用不到，注释下面一行 -->
    <SysUserDepInfo :dep-name="depName" />
  </div>
</template>
<!-- 用户组织关系对象，组织成员 首页 -->
<script>
import crudApi, { getDepMembers } from '@/api/sys/UserDep'
import SysUserDepForm from './form'
import SysUserDepInfo from './info'
import CRUD, { presenter, header } from '@crud/crud'
import CrudOpts from '@crud/CRUD.operation'
import rrOperation from '@crud/RR.operation'
// import udOperation from '@crud/UD.operation'
import udOperation from '@crud/RUD.operation'
import pagination from '@crud/Pagination'
import userList from './userList'
import { getMemberStatuslabel, getSysUserDepDataLevelLabel } from './model'
export default {
  name: 'RightIndex',
  dicts: [
    'sys_user_dep_position_code'
  ], // 字典
  components: {
    CrudOpts,
    rrOperation,
    udOperation,
    pagination,
    SysUserDepInfo,
    SysUserDepForm,
    userList
  },
  cruds() {
    return CRUD({ title: '组织成员',
      initDataMethod: getDepMembers,
      crudMethod: { ...crudApi },
      optShow: {
        add: false
      }})
  },
  mixins: [presenter(), header()],
  props: {
    depId: {
      type: String,
      default: null
    },
    depName: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      tabIndex: '0',
      activeName: 'menbers',
      userId: null,
      username: null,
      nickname: null,
      permission: new Map()
    }
  },
  watch: {
    depId(n, o) {
      if (n) {
        this.crud.refresh()
      }
    }
  },
  mounted() {
    this.permission = this.$store.state.menu.buttonPermission.get('SysDepUser')
  },
  methods: {
    [CRUD.HOOK.beforeRefresh]() {
      this.query.depId = this.depId
      return true
    },
    [CRUD.HOOK.beforeToEdit](crud, data) {
      this.nickname = data.nickname
      this.username = data.username
      this.userId = data.userId
    },
    [CRUD.HOOK.beforeAddCancel]() {
      this.username = null
      this.userId = null
    },
    handleTabClick(tab) {
      this.tabIndex = tab.index
    },
    toAdd() {
      this.activeName = 'users'
      this.tabIndex = 1
    },
    toAddMenuber(userId, username, nickname) {
      this.userId = userId
      this.username = username
      this.nickname = nickname
      this.crud.toAdd()
    },
    getSysUserDepDataLevelLabel(v) {
      return getSysUserDepDataLevelLabel(v)
    },
    getMemberStatuslabel(v) {
      return getMemberStatuslabel(v)
    }
  }
}
</script>

<style scoped>

</style>
